#ifndef WIDGET_H
#define WIDGET_H

#include <QWidget>
#include <QSerialPort>
#include <QTime>
#include <QThread>
#include <QFileDialog>
#include <QTextStream>


QT_BEGIN_NAMESPACE
namespace Ui { class Widget; }
QT_END_NAMESPACE

class Widget : public QWidget
{
    Q_OBJECT

public:
    Widget(QWidget *parent = nullptr);
    ~Widget();
    //定义串口变量
    QSerialPort *serialPort;
    uint8_t opBtSta = 0;

public:
    //CRC校验代码
    quint16 crc16ForModbus(const QByteArray &data);

    //ushort checksum (QByteArray *buffer,int size);

    uint8_t get_checksum(char* dat, uint16_t len);

    //数据发送
    int writeData(char *data, int size);

    //数据接收
    int readData(char *buffer, int count, int timeout);

    //数据转换
    QByteArray QString2Hex(QString str);

    char ConvertHexChar(char ch);

    //延时函数
    void sleep( int msec);

     void setCmbDisb(bool mod);

     void  dispTestResult(uchar *data);

     void dispResult(uchar *data);

     void dispTips(uchar *data);

     void cleanResult();

private slots:
    void on_openBt_clicked();

    //定于接收框的槽函数
    void serialPortReadyRead_Slot();

    void on_cleanResultBt_clicked();

    void on_begainTestBt_clicked();

    void on_sofareUpdateBt_clicked();

    void on_aboutBt_clicked();

    void on_keyModifyBt_clicked();

    void on_shuikongUpdateBt_clicked();

    void on_saveDataBt_clicked();

private:
    Ui::Widget *ui;
};

//添加线程类
class MyThread : public QThread
{
    Q_OBJECT
public:
    MyThread *thread;
    MyThread(Widget * parent, QSerialPort *port);
protected:
    void run();
protected:
    QSerialPort * port;
    Widget * parent;
signals:
    void sendResult(int code, QString str);
};

#endif // WIDGET_H
